/*
 * Context.java
 *
 * Stateroom - Java framework library for finite state machines
 * Copyright (C) 2009 by Matthew Werny
 * All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.iceflow.stateroom;

import net.iceflow.stateroom.enums.Status;
import net.iceflow.stateroom.interfaces.Model;
import net.iceflow.stateroom.interfaces.State;

/**
 * Container for contextual information specific to an individual machine run instance.
 * @param <M> Type used to represent the machine model
 */
public class Context<M extends Model> {

   /** Current state of the context. */
   private State<M> _currentState;

   /** Current status of the context. */
   private Status _status;

   /** Model used by the context. */
   private M _model;

   /**
    * Gets the current state in this machine context after the most recent evaluation cycle.
    * @return Current state in this run context
    */
   public State<M> getCurrentState() {
      // Current state of the machine to return
      State<M> currentState;

      // Prevent other threads from interfering
      synchronized (this) {
         // Extract the current state in a thread-safe manner
         currentState = this._currentState;
      }

      // Return the extracted current state instance
      return currentState;
   }

   /**
    * Sets the current state in this machine context after the most recent evaluation cycle.
    * @param currentState State to be set as the current state in this run context
    */
   public void setCurrentState(final State<M> currentState) {
      // Prevent other threads from interfering
      synchronized (this) {
         // Set the current state in a thread-safe manner
         this._currentState = currentState;
      }
   }

   /**
    * Gets the current status of the machine after the most recent evaluation cycle.
    * @return Status of the machine in this run context
    */
   public Status getStatus() {
      // Current status of the machine to return
      Status status;

      // Prevent other threads from interfering
      synchronized (this) {
         // Extract the current status in a thread-safe manner
         status = this._status;
      }

      // Return the extracted current status
      return status;
   }

   /**
    * Sets the current status of the machine after the most recent evaluation cycle.
    * @param status Status of the machine in this run context
    */
   public void setStatus(final Status status) {
      // Prevent other threads from interfering
      synchronized (this) {
         // Set the status in a thread-safe manner
         this._status = status;
      }
   }

   /**
    * Gets the model used by the context.
    * @return the model
    */
   public M getModel() {
      // Current model to return
      M model;

      // Prevent other threads from interfering
      synchronized (this) {
         // Extract the model in a thread-safe manner
         model = this._model;
      }

      // Return the extracted model
      return model;
   }

   /**
    * Sets the model used by the context.
    * @param model the model to set
    */
   public void setModel(final M model) {
      // Prevent other threads from interfering
      synchronized (this) {
         // Set the model in a thread-safe manner
         this._model = model;
      }
   }
}
